<?php
class Main extends Controller {

    function __construct()
    {
        parent::Controller();
        $this->load->model('Need_model');
        $this->load->model('Stats_model');
    }


    function index ()
    {
        $this->load->helper('form');
        $this->load->helper('date');
        $this->load->helper('string');
        $this->load->helper('url');
        
        //START *** REFRESH DE LAS ESTADÍSTICAS
        $this->load->model('Stats_model');
        if ($this->Stats_model->vejez() > 20)
            {
                $this->load->model('Need_model');
                //uso de set stats : function set_stats($total_solved, $total_helping, $total_waiting, $total_calls)
                $this->Stats_model->set_stats($this->Need_model->count_state("RESUELTO"),$this->Need_model->count_state("RESOLVIENDO"),$this->Need_model->count_state("LLAMAR"), 100);
                //hacer que los RESOLVIENDO viejos vuelvan a LLAMAR
                 if (rand(1,4)==2)
                {
                 $this->Need_model->refresh_states();
                }
            }



        //END *** REFRESH DE LAS ESTADÍSTICAS
        //START *** EL REFRESH DE LOS ESTADOS


        //END *** EL REFRESH DE LOS ESTADOS

        // El uso de get_where : function get_where($status = "", $need = "", $from_time="", $limit = 40)
        $data['data'] = $this->Need_model->get_where("LLAMAR","","",300);
        $data['last_id'] = $this->Need_model->last_id();
        $data['count'] = $this->Need_model->count_state();
        $data['nowtime'] = $this->Need_model->get_time();
        $data['stats'] = $this->Stats_model->get_stats();
        $this->load->view('open_html', $data);
        $this->load->view('main_view', $data);
        $this->load->view('close_html', $data);
    }

    function process ()
    {
        $this->load->library('session');
        $this->load->helper('url');
        //Check if there is a post and set the hash
        if (isset($_POST['send']) || isset($_POST['resolve']))
            {

            //validate
              /*
                 if($this->session->userdata('form_hash') !== md5($_POST['status'].$_POST['need'].$_POST['obs']))

                {*/
                    //Guardar
                    if(isset($_POST['send']))
                    {
                        $this->Need_model->update($_POST['id'], "LLAMAR", $_POST['need'], $_POST['obs']);
                   //     $this->session->set_userdata('form_hash', md5($_POST['status'].$_POST['need'].$_POST['obs']));
                    }
                    //Resuelto
                    if(isset($_POST['resolve']))
                    {
                        $this->Need_model->update($_POST['id'], "RESUELTO", $_POST['need'], $_POST['obs']);
                     //   $this->session->set_userdata('form_hash', md5($_POST['status'].$_POST['need'].$_POST['obs']));
                    }

                //}
        }
           redirect('/main');
    }


}
?>
